package com.zero.mms.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liyi.service.EmpSalaryService;
import com.liyi.service.EmpService;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.EmpSalary;

@Controller("empSalaryControllerLiyi")
@RequestMapping("empsalary")
public class EmpSalaryController {
	
	@Inject
	private EmpSalaryService empSalaryServiceLiyi;
	@Inject
	private EmpService empServiceLiyi;
	
	/* 获取某 [部门] 下所有员工在某 [月] 的所有 [工资项] 的键值数据 */
	@RequestMapping("/get")
	@ResponseBody
	public Object get(Integer depId, Integer month) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			// 获取该部门下的所有员工id
			List<Integer> empIds = empServiceLiyi.getEmpIdsByDepId(depId);
			// 分别获取所有员工id在该月的所有工资项
			List<EmpSalary> empSalaries = new ArrayList<EmpSalary>();
			for (Integer empId : empIds) {
				 empSalaries.addAll(empSalaryServiceLiyi.getESsByEmpIdAndMonth(empId, month));
			}
			
			ArrayNode re = JsonCreator.generateJsonArray();
			if (empSalaries.size() > 0) {
				// 做比较器按order重排显示结果
				Comparator<EmpSalary> comparator = new Comparator<EmpSalary>() {
		            public int compare(EmpSalary es1, EmpSalary es2) {
		                try {
		                	// 先按员工id排序
		                	if (es1.getEmpId() != es2.getEmpId()) {
								return es1.getEmpId() - es2.getEmpId();
							}
		                	// 员工id相同的按order排
		                	else {
								return empSalaryServiceLiyi.getSalaryItemOrderByEmpSalaryId(es1.getEmpSalaryId()) - empSalaryServiceLiyi.getSalaryItemOrderByEmpSalaryId(es2.getEmpSalaryId());
							}
						} catch (SmsRuntimeException e) {
							e.printStackTrace();
						}
						return 0;
		            }
		        };
		        Collections.sort(empSalaries, comparator);
				// 生成表头
				ObjectNode header = JsonCreator.generateJsonObj();
				header.put("emp_id", "员工编号");
				header.put("emp_name", "员工姓名");
				int firstEmpId = empSalaries.get(0).getEmpId();
				for (EmpSalary empSalary : empSalaries) {
					if (firstEmpId == empSalary.getEmpId()) {
						header.put("id_"+empSalary.getSalaryItemId(), empSalaryServiceLiyi.getSalaryItemNameByEmpSalaryId(empSalary.getEmpSalaryId()));
					}
					else {
						break;
					}
				}
				re.add(header);
				// 生成表内容
				List<Integer> empids = new ArrayList<Integer>();
				for (EmpSalary empSalary : empSalaries) {
					empids.add(empSalary.getEmpId());
					if (empids.size() > 1 && empids.get(empids.size()-1) == empids.get(empids.size()-2)) {
						empids.remove(empids.size()-1);
					}
				}
				for (Integer empId : empids) {
					ObjectNode node = JsonCreator.generateJsonObj();
					node.put("emp_id", empId);
					node.put("emp_name", empServiceLiyi.getEmpNameByEmpId(empId));
					for (EmpSalary empSalary : empSalaries) {
						if (empId == empSalary.getEmpId()) {
							if ( "".equals(empSalary.getEmpSalaryNum()) || empSalary.getEmpSalaryNum() == null ) {
								empSalary.setEmpSalaryNum("0");
							}
							node.put("id_"+empSalary.getEmpSalaryId(), empSalary.getEmpSalaryNum());
						}
					}
					re.add(node);
				}
			}
			else {
				re.add(JsonCreator.generateJsonObj().put("tip", "并没有记录 ╮(╯▽╰)╭"));
			}
			
			// 封装所有员工的工资项和服务器响应信息
			objectNode.putPOJO("response", re);
			objectNode.put("message", "获取该部门下所有员工工资成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}

	/* 计算某 [部门] 下所有员工在某 [月] 的所有 [计算工资项] 的值并写入数据库 */
	@RequestMapping("/calculate")
	@ResponseBody
	public Object fuck(Integer depId, Integer month) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			// 获取该部门下的所有员工id
			List<Integer> empIds = empServiceLiyi.getEmpIdsByDepId(depId);
			// 分别计算所有员工id的所有工资项
			
			for (Integer empId : empIds) {
				empSalaryServiceLiyi.insertEmpSalaryItem(empId, month);
				for (int i = 0; i < 2; i++) {
					empSalaryServiceLiyi.calculate(empId, month);
				}
			}
			
			objectNode.put("message","");
			objectNode.put("response","");
			objectNode.put("statusCode",SmsStatusCode.SUC_CODE);
			
			// return get(depId, month);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
	
	/* 设置某 [员工] 的 在某 [月] 的 [工资项] 状态为 发放 */
	@RequestMapping("/distribute")
	@ResponseBody
	public Object distribute(Integer empId, Integer month) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			empSalaryServiceLiyi.distribute(empId, month);
			objectNode.putPOJO("response", null);
			objectNode.put("message", "发放该员工的工资成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			
			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
	
	/* 获取 [员工工资项] 的改动 [工资值] 并写入数据库 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(Integer empSalaryId, Integer empSalaryNum, Integer empId, Integer month) {
		// 声明返回的JSON对象
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			empSalaryServiceLiyi.save(empSalaryId, empSalaryNum);
			empSalaryServiceLiyi.calculate(empId, month);
			objectNode.putPOJO("response", null);
			objectNode.put("message", "暂存员工工资成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			
			// System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		// 返回JSON对象
		return objectNode;
	}
}
